package com.wfc.servlet.dish;

import com.wfc.entity.Cook;
import com.wfc.entity.Dish;
import com.wfc.service.DishService;
import com.wfc.service.serviceImpl.CookServiceImpl;
import com.wfc.service.serviceImpl.DishServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/updateDish")
public class updateDish extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id1 = req.getParameter("id");
        int id = 0;
        if (!"null".equals(id1)){
            id = Integer.parseInt(id1);
        }
        HttpSession session = req.getSession();

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        DishService dishService = new DishServiceImpl();
        Dish dish = dishService.selectById(id);
        session.setAttribute("xdish",dish);
        session.setAttribute("category",dish.getCategory());
        //根据菜品名查菜谱
        CookServiceImpl cookService = new CookServiceImpl();
        System.out.println(dish.getDname());
        Cook cook = cookService.selectBydname(dish.getDname());

        session.setAttribute("xcook",cook);
        session.setAttribute("state","修改");
        System.out.println(cook.getDname());


        session.setAttribute("xid",id);

        session.setAttribute("xcid",cook.getId());
        //对cook的某些字段进行调整
        //菜谱的制作图片
        if(!"null".equals(cook)){
            String cooking = cook.getCooking();
            String[] imgs = cooking.split("&");
            session.setAttribute("cookingimgs",imgs);
            //菜谱的主食
            String mainfood = cook.getMainfood();
            String[] mainfoods = mainfood.split("&");
            session.setAttribute("mainfoods",mainfoods);
            //菜谱的辅食
            String fufood = cook.getFufood();
            String[] fufoods = fufood.split("&");
            session.setAttribute("fufoods",fufoods);
            //菜谱的调料
            String tiaofood = cook.getTiaofood();
            String[] tiaofoods = tiaofood.split("&");
            session.setAttribute("tiaofoods",tiaofoods);
            //菜谱的 酸辣。。。。
            String process = cook.getProcess();
            String[] processs = process.split("&");
            session.setAttribute("processs",processs);
            //菜谱的制作步骤
            String cookText = cook.getCooktext();
            String[] cookTexts = cookText.split("&");
            session.setAttribute("cookTexts",cookTexts);
        }

    }
}
